package name.wilbur.course.db.bigone.model;
/**
 * @author wilbur
 */

import java.sql.Timestamp;

import name.wilbur.course.db.bigone.util.IMD5;
import name.wilbur.course.db.bigone.util.MyMD5;
import name.wilbur.course.db.bigone.util.MysqlTimestampWrapper;

public class User extends BaseDBModel {
	private String name;
	private String userPassword;
	private Timestamp registerDate;
	private String email;
	private boolean admin;
	
	public User() {
	}
	
	/* Create a new user: set registerDate and encrypt userPasswd */
	public User(String name, String userPasswd, String email, boolean admin) {
		IMD5 md5 = new MyMD5();
		this.name = name;
		this.userPassword = md5.encrypt(userPasswd);
		this.registerDate = MysqlTimestampWrapper.getCurrentTimestamp();
		this.email = email;
		this.admin = admin;
	}
	
	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPasswd) {
		this.userPassword = userPasswd;
	}

	public Timestamp getRegisterDate() {
		return registerDate;
	}

	public void setRegisterDate(Timestamp registerDate) {
		this.registerDate = registerDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean getAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((registerDate == null) ? 0 : registerDate.hashCode());
		result = prime * result
				+ ((userPassword == null) ? 0 : userPassword.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (registerDate == null) {
			if (other.registerDate != null)
				return false;
		} else if (!registerDate.equals(other.registerDate))
			return false;
		if (userPassword == null) {
			if (other.userPassword != null)
				return false;
		} else if (!userPassword.equals(other.userPassword))
			return false;
		return true;
	}
	
	public String toString() {
		return name;
	}
}
